/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.core.runtime;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.util.ArrayDeque;

/**
 * Provide management to current runtime level.
 *
 * @author Xiaohai Zhang
 * @since 2013-08-24 00:46
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
abstract public class CurrentRuntimeLevel {
    private static final ThreadLocal<ArrayDeque<RuntimeLevel>> current = new ThreadLocal<>();

    private static final RuntimeLevel specified;

    static {
        RuntimeLevel runtimeLevel;
        String name = System.getProperty("runtime.level");
        try {
            runtimeLevel = RuntimeLevel.valueOf(name);
        } catch (Exception ex) {
            CurrentRuntimeLevel.log.warn("No system property 'runtime.level' found, set runtime level to 'TEST'");
            runtimeLevel = RuntimeLevel.TEST;
        }
        specified = runtimeLevel;
    }

    /**
     * Get the current system runtime level.
     */
    public static RuntimeLevel current() {
        ArrayDeque<RuntimeLevel> deque = current.get();
        if (deque != null && !deque.isEmpty()) {
            return deque.peekLast();
        }
        return specified;
    }

    public static boolean lt(RuntimeLevel runtimeLevel) {
        Assert.notNull(runtimeLevel);
        return current().getLevel() < runtimeLevel.getLevel();
    }

    public static boolean le(RuntimeLevel runtimeLevel) {
        Assert.notNull(runtimeLevel);
        return current().getLevel() <= runtimeLevel.getLevel();
    }

    public static boolean gt(RuntimeLevel runtimeLevel) {
        Assert.notNull(runtimeLevel);
        return current().getLevel() > runtimeLevel.getLevel();
    }

    public static boolean ge(RuntimeLevel runtimeLevel) {
        Assert.notNull(runtimeLevel);
        return current().getLevel() >= runtimeLevel.getLevel();
    }

    public static void setRuntimeLevel(RuntimeLevel runtimeLevel) {
        Assert.notNull(runtimeLevel);
        synchronized (current) {
            ArrayDeque<RuntimeLevel> deque = current.get();
            if (deque == null) {
                deque = new ArrayDeque<>();
                current.set(deque);
            }
            deque.addLast(runtimeLevel);
        }
    }

    public static void unsetRuntimeLevel() {
        synchronized (current) {
            ArrayDeque<RuntimeLevel> deque = current.get();
            if (deque != null) {
                deque.removeLast();
                if (deque.isEmpty()) {
                    current.remove();
                }
            }
        }
    }
}
